﻿package mortal.game.view.guildNew.data
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.cache.guild.module.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.daily.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class GuildNewUtil extends Object
    {
        public static var GuildManorMapId:int = 40001;
        public static var watachGuildId:int;
        public static const freeGuildNum:int = 2;
        public static var typeToIndex:Object = {1:0, 8:1, 5:2, 7:3, 3:4, 2:5};
        public static var indexToAttName:Array = ["skillLife", "skillDefense", "skillToughness", "skillHit", "skillBlock", "skillAttack", "skillPenetration", "skillCrit", "skillJouk", "skillExpertise"];
        public static var attNameToResource:Object = {skillLife:9, skillDefense:2, skillToughness:5, skillHit:8, skillBlock:3, skillAttack:4, skillPenetration:1, skillCrit:0, skillJouk:7, skillExpertise:6};

        public function GuildNewUtil()
        {
            return;
        }// end function

        public static function getAllBuildByLevel(param1:int) : Object
        {
            var _loc_4:Dictionary = null;
            var _loc_5:TGuildStructureConfig = null;
            var _loc_2:* = param1;
            var _loc_3:Object = {};
            while (_loc_2 > 0)
            {
                
                _loc_4 = GuildBuildingConfig.instance.getAllBuildingList(_loc_2);
                for each (_loc_5 in _loc_4)
                {
                    
                    if (!_loc_3[_loc_5.structureType])
                    {
                        _loc_3[_loc_5.structureType] = _loc_5;
                    }
                }
                _loc_2 = _loc_2 - 1;
            }
            return _loc_3;
        }// end function

        public static function getCurAltarCurLAndNeedL() : Array
        {
            var _loc_6:TGuildStructureConfig = null;
            var _loc_1:int = 0;
            var _loc_2:int = 7;
            var _loc_3:* = Cache.instance.guild.selfGuildInfo.baseInfo.level;
            var _loc_4:* = getAllBuildByLevel(_loc_3);
            if (_loc_4)
            {
                _loc_6 = _loc_4[_loc_2];
                if (_loc_6)
                {
                    _loc_1 = _loc_6.structureLevel;
                }
            }
            var _loc_5:* = GuildBuildingConfig.instance.getNextBuildingLvNeedGuildLv2(_loc_3, _loc_2, _loc_1);
            return [_loc_1, _loc_5];
        }// end function

        public static function getCurMaxSkillIdArr() : Array
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            var _loc_2:* = GuildBuildingConfig.instance.getInfoByBuildingType(_loc_1.baseInfo.level, EGuildStructureType._EGuildSchool).structureLevel;
            var _loc_3:* = GuildSchoolConfig.instance.getInfoByLevel(_loc_2);
            var _loc_4:Array = [];
            var _loc_5:int = 0;
            while (_loc_5 < indexToAttName.length)
            {
                
                _loc_4.push(_loc_3[indexToAttName[_loc_5]]);
                _loc_5++;
            }
            return _loc_4;
        }// end function

        public static function getCurSchoolLevel() : int
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_1.baseInfo == null)
            {
                return 0;
            }
            var _loc_2:* = GuildBuildingConfig.instance.getInfoByBuildingType(_loc_1.baseInfo.level, EGuildStructureType._EGuildSchool);
            if (_loc_2 == null)
            {
                return 0;
            }
            var _loc_3:* = _loc_2.structureLevel;
            return _loc_3;
        }// end function

        public static function getSkillLearnArr() : Array
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            var _loc_2:Array = [];
            _loc_2[0] = getCurSchoolLv();
            _loc_2[1] = _loc_1.selfInfo.contribution.toString();
            _loc_2[2] = GuildConst.GuildSkillLearnCount() - _loc_1.selfInfo.skillTimes;
            return _loc_2;
        }// end function

        public static function getSkillLeft() : int
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_1 == null)
            {
                return 0;
            }
            return GuildConst.GuildSkillLearnCount() - _loc_1.selfInfo.skillTimes;
        }// end function

        public static function getCurSchoolInfo() : TGuildStructureConfig
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            var _loc_2:* = getAllBuildByLevel(_loc_1.baseInfo.level);
            var _loc_3:* = _loc_2[EGuildStructureType._EGuildSchool] as TGuildStructureConfig;
            return _loc_3;
        }// end function

        public static function getCurSchoolLv() : int
        {
            var _loc_1:* = getCurSchoolInfo();
            if (_loc_1)
            {
                return _loc_1.structureLevel;
            }
            return 0;
        }// end function

        public static function getFirstSchoolLevel(param1:String) : int
        {
            var _loc_4:TGuildSchoolTarget = null;
            var _loc_2:* = int(GuildSchoolConfig.instance.maxKey);
            var _loc_3:int = 1;
            while (_loc_3 <= _loc_2)
            {
                
                _loc_4 = GuildSchoolConfig.instance.getInfoByLevel(_loc_3);
                if (_loc_4[param1] > 0)
                {
                    return _loc_3;
                }
                _loc_3++;
            }
            return 0;
        }// end function

        public static function getCurSchSkillIdByAttName(param1:String) : int
        {
            var _loc_2:* = getCurSchoolLevel();
            var _loc_3:* = GuildSchoolConfig.instance.getInfoByLevel(_loc_2);
            if (_loc_3 == null)
            {
                return 0;
            }
            return int(_loc_3[param1]);
        }// end function

        public static function hasSkillToUpgrate() : Boolean
        {
            var _loc_6:TSkill = null;
            var _loc_7:SkillInfo = null;
            var _loc_8:Boolean = false;
            var _loc_9:String = null;
            var _loc_10:int = 0;
            if (Cache.instance.guild.selfGuildInfo == null || Cache.instance.guild.selfGuildInfo.selfHasJoinGuild == false)
            {
                return false;
            }
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_1.selfInfo == null)
            {
                return false;
            }
            var _loc_2:* = GuildBuildingConfig.instance.getInfoByBuildingType(_loc_1.selfInfo.level, EGuildStructureType._EGuildSchool);
            if (_loc_2 == null)
            {
                return false;
            }
            var _loc_3:* = _loc_2.structureLevel;
            var _loc_4:* = GuildSchoolConfig.instance.getInfoByLevel(_loc_3);
            if (_loc_4 == null)
            {
                return false;
            }
            var _loc_5:* = GuildNewUtil.indexToAttName;
            for each (_loc_9 in _loc_5)
            {
                
                _loc_10 = getNextSkillContribute(_loc_9);
                if (_loc_10 != 0 && _loc_1.selfInfo.contribution >= _loc_10)
                {
                    _loc_8 = true;
                    break;
                }
            }
            return _loc_8;
        }// end function

        private static function getNextSkillContribute(param1:String) : int
        {
            var _loc_2:* = getCurSchSkillIdByAttName(param1);
            if (_loc_2 == 0)
            {
                return 0;
            }
            var _loc_3:* = new SkillInfo();
            _loc_3.tSkill = SkillConfig.instance.getInfoById(_loc_2);
            var _loc_4:* = new SkillInfo();
            var _loc_5:* = Cache.instance.skill.getSkillBySerialId(_loc_3.tSkill.series);
            if (_loc_5 == null)
            {
                _loc_4.tSkill = SkillConfig.instance.getFirstSkillBySerialId(_loc_3.tSkill.series);
            }
            else
            {
                if (_loc_5.tSkill.skillLevel >= _loc_3.tSkill.skillLevel)
                {
                    return 0;
                }
                _loc_4.tSkill = SkillConfig.instance.getSkillByLevel(_loc_5.tSkill.series, (_loc_5.tSkill.skillLevel + 1));
            }
            return _loc_4.tSkill.needGuildContribute;
        }// end function

        public static function getActiveArr() : Array
        {
            var _loc_1:Array = [];
            _loc_1 = DailyUtil.getDataByGroup(EDailyType._EDailyTypeGuild);
            _loc_1.sortOn("guildActiveRank", Array.NUMERIC);
            return _loc_1;
        }// end function

        public static function getAllRecord() : Vector.<SGuildWarehouseRecord>
        {
            var _loc_2:Vector.<SGuildWarehouseRecord> = null;
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.wareHouse;
            _loc_2 = _loc_1.recordVec;
            return _loc_2;
        }// end function

        public static function getManorInfo() : void
        {
            var _loc_1:* = new Object();
            _loc_1.camp = 0;
            _loc_1.guildName = "";
            _loc_1.startIdx = 0;
            _loc_1.isfull = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_SEARCH, _loc_1));
            return;
        }// end function

        public static function hadIndexFruits() : int
        {
            var _loc_6:SMiniGuildInfo = null;
            var _loc_7:Date = null;
            var _loc_1:* = Cache.instance.guild;
            var _loc_2:* = ClockManager.instance.nowDate;
            var _loc_3:int = -1;
            var _loc_4:* = _loc_1.guildList.length;
            if (_loc_1.selfGuildInfo.baseInfo == null)
            {
                return -1;
            }
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_1.guildList[_loc_5];
                _loc_7 = _loc_6.nextPickUpDt;
                if (_loc_2.time >= _loc_7.time && _loc_1.selfGuildInfo.baseInfo.level >= 3)
                {
                    _loc_3 = _loc_5;
                    break;
                }
                _loc_5++;
            }
            return _loc_3;
        }// end function

        public static function canSendGuildMail(param1:Boolean = true) : Boolean
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_2.baseInfo.level < 4)
            {
                if (param1)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71436));
                }
                return false;
            }
            if (!GuildConst.hasPermissions(GuildConst.SendGuildMail))
            {
                if (param1)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71437));
                }
                return false;
            }
            var _loc_3:* = Cache.instance.role.entityInfo.combat;
            var _loc_4:int = 30000;
            if (_loc_3 < _loc_4)
            {
                if (param1)
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(71438, _loc_4));
                }
                return false;
            }
            return true;
        }// end function

    }
}
